<?php

class RegularTransactionsController extends AppController {

    var $name = 'RegularTransactions';

    function index() {
        $this->RegularTransaction->recursive = 0;
        $this->set('regularTransactions', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid regular transaction', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('regularTransaction', $this->RegularTransaction->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->RegularTransaction->create();
            if ($this->RegularTransaction->save($this->data)) {
                $this->Session->setFlash(__('The regular transaction has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The regular transaction could not be saved. Please, try again.', true));
            }
        }
        $users = $this->RegularTransaction->User->find('list');
        $accounts = $this->RegularTransaction->Account->find('list');
        $this->set(compact('users', 'accounts'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid regular transaction', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->RegularTransaction->save($this->data)) {
                $this->Session->setFlash(__('The regular transaction has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The regular transaction could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->RegularTransaction->read(null, $id);
        }
        $users = $this->RegularTransaction->User->find('list');
        $accounts = $this->RegularTransaction->Account->find('list');
        $this->set(compact('users', 'accounts'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for regular transaction', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->RegularTransaction->delete($id)) {
            $this->Session->setFlash(__('Regular transaction deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Regular transaction was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }
}
